package com.xiyuan.almighty.service;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @创建人 袁继鑫
 * @创建时间 2021-03-08 13:53
 * @版本 1.0
 * @描述
 */
public class TestABCAlternate {
    public static void main(String[] args) {
        AtomicInteger count = new AtomicInteger(0);
        count.incrementAndGet();
        AlternateDemo ad = new AlternateDemo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 20; i++) {
                    ad.loopA(i);
                }
            }

        }, "A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 20; i++) {
                    ad.loopB(i);
                }
            }

        }, "B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 20; i++) {
                    ad.loopC(i);
                    System.out.println("-----------------------------------");
                }
            }

        }, "C").start();

        Thread thread = new Thread();
        Thread.State state = thread.getState();
        System.out.println("state = " + state);

    }
}

class AlternateDemo {
    private int number = 1;//当前正在执行线程的标记
    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();

    /*
     * @param totalLoop:循环第几轮
     */
    public void loopA(int totalLoop) {
        lock.lock();
        try {
            //1.判断
            if (number != 1) {
                condition1.await();
            }

            //2.打印
            System.out.println(Thread.currentThread().getName() + "\t" + totalLoop);

            //3.唤醒
            number = 2;
            condition2.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void loopB(int totalLoop) {
        lock.lock();
        try {
            //1.判断
            if (number != 2) {
                condition2.await();
            }

            //2.打印
            System.out.println(Thread.currentThread().getName() + "\t" + totalLoop);

            //3.唤醒
            number = 3;
            condition3.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void loopC(int totalLoop) {
        lock.lock();
        try {
            //1.判断
            if (number != 3) {
                condition3.await();
            }

            //2.打印
            System.out.println(Thread.currentThread().getName() + "\t" + totalLoop);

            //3.唤醒
            number = 1;
            condition1.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}